// Copyright (C) 2005 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//  	$Id: Main.java 118 2005-08-13 03:59:08Z richetl $	
//

import antlr.collections.AST;
import java.io.*;
import java.util.*;

class Main
{

    private static final String VERSION = "0.0.5";

    public static void main(String[] args) {

	try {
	    System.out.println("llt v" + VERSION + " for " +
			       System.getProperty("os.name") + " " + 
			       System.getProperty("os.arch"));

	    // Read in filename from command line arguments
	    // and create lexer from input file

	    String filename = processArgs(args);
	    BufferedReader in = null;
	    try {
		in = new BufferedReader(new FileReader(filename));
	    }
	    catch (FileNotFoundException e) {
		System.err.println("\nERROR: Specified file \"" + filename + 
				   "\" does not exist.");
		System.err.println();
		System.exit(1);
	    }

	    LltLexer lexer = new LltLexer(in);
	    System.out.println("Translating file: " + filename);

	    // Create parser object and parse the input

	    LltParser parser = new LltParser(lexer);
	    parser.compilation_unit();

	    // Get the AST generated by the parser and
	    // start translation

	    AST t = parser.getAST();
	    LltTreeParser translator = new LltTreeParser();
	    translator.compilation_unit(t);
	}
	catch (Exception e) {             
             System.err.print("ERROR: ");
             System.err.println(e);
        }
    }

    private static String processArgs(String[] args) {
	List argList = (List)Arrays.asList(args);
	String filename = "";
	if(argList.isEmpty()) {
	    System.err.println("No file name found -- exiting");
	    System.exit(1);
	}
	else {
	    filename = (String)argList.get(0);
	}
	return filename;
    }
}
